{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-03-05T06:42:22.477130Z",
     "start_time": "2024-03-05T06:42:22.473487Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "William\n",
      "John\n",
      "Wang\n"
     ]
    }
   ],
   "source": [
    "# for循环\n",
    "friends = ['william', 'john', 'wang']\n",
    "for friend in friends:\n",
    "    print(friend.capitalize()) # 名字首字母大写"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name\n",
      "language\n",
      "Sessions\n",
      "Other name\n"
     ]
    }
   ],
   "source": [
    "movie1 = {\"name\": \"Friends\", \"language\": \"Python\", \"Sessions\": 10, \"Other name\": \"Six of One\"}\n",
    "# 遍历字典的“键”\n",
    "for title in movie1.keys():\n",
    "    print(title)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-05T06:48:59.132536Z",
     "start_time": "2024-03-05T06:48:59.126710Z"
    }
   },
   "id": "98ec2862924a2fed",
   "execution_count": 2
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Friends\n",
      "Python\n",
      "10\n",
      "Six of One\n"
     ]
    }
   ],
   "source": [
    "for value in movie1.values():\n",
    "    print(value)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-05T06:49:56.981891Z",
     "start_time": "2024-03-05T06:49:56.975946Z"
    }
   },
   "id": "6ec94972e350874f",
   "execution_count": 3
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name Friends\n",
      "language Python\n",
      "Sessions 10\n",
      "Other name Six of One\n"
     ]
    }
   ],
   "source": [
    "for title, value in movie1.items():\n",
    "    print(title, value)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-05T06:50:23.908123Z",
     "start_time": "2024-03-05T06:50:23.902691Z"
    }
   },
   "id": "d11bac3ea26d0fb9",
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, ('name', 'Friends'))\n",
      "(1, ('language', 'Python'))\n",
      "(2, ('Sessions', 10))\n",
      "(3, ('Other name', 'Six of One'))\n"
     ]
    }
   ],
   "source": [
    "for i in enumerate(movie1.items()):\n",
    "    print(i)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-05T06:56:31.522859Z",
     "start_time": "2024-03-05T06:56:31.517858Z"
    }
   },
   "id": "20cf75b039fbbdc1",
   "execution_count": 5
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Friends\n",
      "Python\n",
      "10\n",
      "Six of One\n"
     ]
    }
   ],
   "source": [
    "for i in enumerate(movie1.items()):\n",
    "    # print(i[0])\n",
    "    # print(i[1])\n",
    "    # print(i[1][0])\n",
    "    print(i[1][1])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-05T06:59:42.912279Z",
     "start_time": "2024-03-05T06:59:42.906917Z"
    }
   },
   "id": "500fa219acc159ad",
   "execution_count": 11
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "# 推导式\n",
    "list1 = [i for i in (1,2,3,4)]\n",
    "print(list1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-05T07:02:20.281426Z",
     "start_time": "2024-03-05T07:02:20.277404Z"
    }
   },
   "id": "adfa6e99cc06cdb5",
   "execution_count": 14
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9, 16]\n"
     ]
    }
   ],
   "source": [
    "list2 = [i * i for i in (1,2,3,4)]\n",
    "print(list2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-05T07:02:42.273349Z",
     "start_time": "2024-03-05T07:02:42.269289Z"
    }
   },
   "id": "3668f30c305bd590",
   "execution_count": 16
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[9, 16]\n"
     ]
    }
   ],
   "source": [
    "list3 = [ i * i for i in (1,2,3,4) if i > 2]\n",
    "print(list3)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-05T07:16:41.095957Z",
     "start_time": "2024-03-05T07:16:41.091572Z"
    }
   },
   "id": "22848a4401ac24c5",
   "execution_count": 24
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[9, 16, 9, 16]\n"
     ]
    }
   ],
   "source": [
    "for i in (1, 2, 3, 4):\n",
    "    if i > 2:\n",
    "        list3.append(i * i)\n",
    "print(list2)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "89a7ef08e772e027",
   "execution_count": 22
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 练习\n",
    "# [ \"rachel\", \"monica\", \"Phoebe\", \"joey\" ]\n",
    "\"\"\"\n",
    "需要将该列表按照首字母升序进行排列。\n",
    "需将该列表中的每个元素全部改为大写字母。\n",
    "需输出排序后的序号和列表内容。\n",
    "\"\"\" "
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "e8fc64d13c316c4e"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
